#include "ProfileLoader.h"

#pragma warning(disable:4390)
#pragma warning(disable:4305)

ProfileLoader::ProfileLoader(void)
{
	this->profile = new Profile();
}

ProfileLoader::~ProfileLoader(void)
{
	delete this->profile;
}

void ProfileLoader::parseProfileFile(const Ogre::String &fileName, const Ogre::String &groupName) {
	rapidxml::xml_document<> XMLDoc;
	rapidxml::xml_node<>* XMLRoot;

	Ogre::DataStreamPtr stream = Ogre::ResourceGroupManager::getSingleton().openResource(fileName, groupName);
    char* prof = strdup(stream->getAsString().c_str());
    XMLDoc.parse<0>(prof);

	XMLRoot = XMLDoc.first_node("profile");

	processProfile(XMLRoot);
	//delete prof;
}

void ProfileLoader::processProfile(rapidxml::xml_node<>* XMLRoot) {
	if (XMLRoot->first_attribute("name"))
		this->profile->name = XMLRoot->first_attribute("name")->value();

	rapidxml::xml_node<>* pElement;
	pElement = XMLRoot->first_node("difficulty");
    if(pElement)
        processDifficulty(pElement);

	pElement = XMLRoot->first_node("progress");
    if(pElement)
        processProgress(pElement);
}

void ProfileLoader::processDifficulty(rapidxml::xml_node<> *XMLNode) {
	if (XMLNode->value())
		this->profile->difficulty = XMLNode->value();
}

void ProfileLoader::processProgress(rapidxml::xml_node<> *XMLNode) {
	if (XMLNode->first_attribute("level"))
		this->profile->progressLevel = Ogre::StringConverter::parseInt(XMLNode->first_attribute("level")->value());

	if (XMLNode->first_attribute("sublevel"))
		this->profile->progressSublevel = Ogre::StringConverter::parseInt(XMLNode->first_attribute("sublevel")->value());
}
